<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/9/21
 * Time: 下午9:04
 */
class RealService extends Service {

    public static function getHeader($params){
        $header = array(
            'date'   => array('text' => '日期'),
        );

        $end = strtotime(date('Y-m-d'));
        if(strtotime($params['time_end']) < $end){
            $begin = $end + 86400 - 300;
        }else{
            $begin  = floor(time() / 300) * 300;
        }

        $begin = $begin + 60;
        for(; $begin>=$end; $begin-=300){
            $min = date('H:i', $begin);
            
            $header[$min] = array('text' => $min);
        }
 
        
        
        return $header;
    }

    public static function getData($type, $params){
    	$params['server'] = $params['server_online'];
        $model = new ModReal();
        $data = $model->selectByType($type, $params);
        
        $result = array();

        $begin  = strtotime($params['time_start']);
        $end    = strtotime($params['time_end']) + 86400;
        $now    = time();
        for(;$begin<$end;$begin+=300){
            $day = date('Y-m-d', $begin);
            if(!isset($result[$day])){
                $result[$day] = array(
                    'date' => $day
                );
            }
            //$result[$day][date('H:i', $begin)] = ($begin > $now) ? '-' : 0;
        }
        
        $header = array(
        		'date'   => array('text' => '日期'),
        );

        foreach($data as $v){
            $timestamp = $v['ctime'];
            $day = date('Y-m-d', $timestamp);

            $result[$day][date('H:i', $timestamp)] = $v['online_num'];
            
            $header[date('H:i', $timestamp)] = array('text' => date('H:i', $timestamp));
            
        }
        $h_d_result = array('header'=>$header,'data'=>$result);
        return $h_d_result;
    }
    
    public static function getHeader1($params){
    	$header = array(
    			'date'   => array('text' => '日期'),
    	);
    
    	$end = strtotime(date('Y-m-d'));
    	if(strtotime($params['time_end']) < $end){
    		$begin = $end + 86400 - 300;
    		
    	}else{
    		$begin  = floor(time() / 300) * 300;
    		$begin = $begin - 20*60;
    	}

    	for(; $begin>=$end; $begin-=300){
    		$min = date('H:i', $begin);
    
    		$header[$min] = array('text' => $min);
    	}
    
    
    
    	return $header;
    }
    
    
    public static function getData1($type, $params){     	
//     	if(empty($params['time_start'])){
//     		$time_array = explode('~', $params['time']);
//     		$params['time_start'] = $time_array[0];
//     		$params['time_end'] = $time_array[1];
//     		$params['time_start'] = str_replace(".","-",$params['time_start']);
//     		$params['time_end'] = str_replace(".","-",$params['time_end']);
//     	}  	   	
    	
    	$model = new ModReal();
    	$data = $model->selectByType1($type, $params);
    	
    	$new_data = array();
    	foreach ($data as $key=>$val){
    		$new_data[$val['RecDate']]['RecDate'] = $val['RecDate'];
    		$new_data[$val['RecDate']]['Value'] += $val['Value'];
    	}
    	
    
    	$result = array();
    
    	$begin  = strtotime($params['time_start']);
    	$end    = strtotime($params['time_end']) + 86400;
    	$now    = time();
    	for(;$begin<$end;$begin+=300){
    		$day = date('Y-m-d', $begin);
    		if(!isset($result[$day])){
    			$result[$day] = array(
    					'date' => $day
    			);
    		}
    		$result[$day][date('H:i', $begin)] = ($begin > $now) ? '-' : 0;
    	}
    	
    	foreach($new_data as $v){
    		$timestamp = $v['RecDate'];
    		$day = date('Y-m-d', $timestamp);
    
    		$result[$day][date('H:i', $timestamp)] = $v['Value'];
    	}
//     	echo '<pre/>';
//     	var_dump($result);exit;
    	return $result;
    }

    public static function getChart($params, $data){
        // 取最近的三天画图
        $endTime = strtotime($params['time_end']);
        $dateArr = array(
            date('Y-m-d', $endTime-86400*6), date('Y-m-d', $endTime-86400*5),
            date('Y-m-d', $endTime-86400*4), date('Y-m-d', $endTime-86400*3),
            date('Y-m-d', $endTime-86400*2), date('Y-m-d', $endTime-86400), $params['time_end']);

        $chartData = $series = array();
        foreach($dateArr as $day){
            if(strtotime($day) >= strtotime($params['time_start'])){
                $tmp = $data[$day];
                unset($tmp['date']);

                $chartData[$day] = $tmp;

                $series[$day] = array('symbolSize' => 0);
            }
        }

        return array(
            'data'      => $chartData,
            'setting'   => array(
                'series' => $series
            )
        );

    }

}